package com.binvshe.binvshe.opus;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.entity.StoryMenuEntity;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.pachong.android.baseuicomponent.activity.SwipeListActivity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2017/2/4
 */

public class LongStoryMenuActivity extends SwipeListActivity {

    private static final String KEY_TITLE = "TITLE";
    private static final String KEY_ARTID = "ARTID";

    @InjectExtra(KEY_TITLE)
    String mTitle;
    @InjectExtra(KEY_ARTID)
    int mArtID;
    private int mPage = 0;
    ListDataRequestListener<StoryMenuEntity> mRequestListener = new ListDataRequestListener(this, StoryMenuEntity.class, true);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Dart.inject(this);
        enableBackButton();
        setPullToRefreshEnable(false);
        getCustomToolbar().setTitle(mTitle);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startLoading();
    }


    public static void start(Context context, int artid, String title) {
        Intent starter = new Intent(context, LongStoryMenuActivity.class);
        starter.putExtra(KEY_TITLE, title);
        starter.putExtra(KEY_ARTID, artid);
        context.startActivity(starter);
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new MenuAdapter(this);
    }

    @Override
    public void onStartLoading() {
        getMenuData();
    }

    private void getMenuData() {
        if (mPage == 0 || mRequestListener.getResponseDataParser().hasMore()) {
            mPage++;
            IHttpRequest request = new MyHttpRequest(this);
            UrlParams params = new UrlParams();
            params.put("page", mPage + "");
            params.put("size", 10 + "");
            params.put("article_id", mArtID + "");
            request.get(UrlCenter.GET_CHAPTER_LIST, params, mRequestListener);
        }
    }


    class MenuAdapter extends RecyclerViewBaseAdapter<StoryMenuEntity> {


        public MenuAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new MenuHolder(setAdapterViewItem(parent, R.layout.item_story_menu));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
            MenuHolder vh = (MenuHolder) holder;
            StoryMenuEntity data = this.getData().get(position);
            vh.mTvTitle.setText(data.getTitle());

            setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    StoryDetailsActivity.start(getContext(), mArtID,
                            getData().get(i).getId(), getData().get(i).getTitle());
                }
            });
        }

        class MenuHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tvTitle)
            TextView mTvTitle;

            public MenuHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

    }
}
